package com.question.business.dict.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.question.business.dict.entity.Dict;
import com.question.business.dict.mapper.DictMapper;
import com.question.business.dict.model.ao.DictAddAO;
import com.question.business.dict.model.vo.DictDetailVO;
import com.question.business.dict.service.IDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 字典服务实现类
 *
 * @author star
 * @since 2023-09-07
 */
@Slf4j
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {


    /**
     * 字典分页查询
     *
     * @param typeId 类型ID
     * @return Page<DictDetailVO> 响应参数
     */
    @Override
    public List<DictDetailVO> queryDictList(String typeId) {
        return baseMapper.queryDictList(typeId);
    }

    /**
     * 字典新增数据
     *
     * @param typeId 类型ID
     * @param ao     请求参数
     * @return List<DictAddAO> 响应参数
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addDict(String typeId, List<DictAddAO> ao) {
        // 删除历史字典
        baseMapper.deleteByTypeId(typeId);
        if (CollectionUtils.isNotEmpty(ao)) {
            List<Dict> data = new ArrayList<>(ao.size());
            for (DictAddAO dict : ao) {
                Dict entity = new Dict();
                entity.setTypeId(typeId);
                entity.setDictCode(dict.getDictCode());
                entity.setDictDesc(dict.getDictDesc());
                entity.setDictOrder(dict.getDictOrder());
                data.add(entity);
            }
            super.saveBatch(data);
        }
    }

}